/**
 * @file helper.js
 * @author lili39@didichuxing.com
 * @date 2018-08-28 20:52:16
 */

import uuid from 'uuid';

export const assignProperties = data => {
    return data.map((node, idx) => {
        node.id = uuid.v4();
        node.idx = idx;
        node._display = typeof node.display === 'boolean' ? node.display : true;
        node._collapsed = false;
        if (node.children && node.children.length > 0) {
            node.children = assignProperties(node.children);
            node._children = node.children;
        }
        return node;
    });
};

export const uniqueId = () => {
    return Math.random()
        .toString(36)
        .substring(7);
};

/**
 * @description 判断数据类型
 * @param {*} obj
 */
export const typeOf = obj => {
    const toString = Object.prototype.toString;
    const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object'
    };
    return map[toString.call(obj)];
};
